<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="导航名称" prop="moduleName">
        <el-input v-model="dataForm.moduleName" placeholder="导航名称"></el-input>
      </el-form-item>
      <el-form-item label="跳转链接" prop="moduleId">
        <el-select
          style="width:100%"
          filterable
          v-model="dataForm.moduleId"
          placeholder="模块名称"
          @change="moduleChange"
        >
          <el-option
            v-for="item in moduleList"
            :key="item.id"
            :label="item.moduleName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="跳转详情"
        prop="params"
        v-if="visible && selectedModule && selectedModule.isParams === 1"
      >
        <el-input readonly v-model="dataForm.paramsName" disabled/>
        <news-category-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/news/news'"
        />
        <news-detail-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/news/news-detail'"
        />
        <course-detail-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/course/detail'"
        />
        <exam-cate-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/exam/exam-list'"
        />
        <daily-cate-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/exam/test-list'"
        />
        <exam-detail-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/exam/exam-detail'"
        />
        <deily-detail-selector
          @onSelect="selectParams"
          :selected-id="dataForm.params"
          v-if="selectedModule.modulePath === '/pages/deily/deilydetail'"
        />
      </el-form-item>
      <el-form-item label="模块图片" prop="moduleImageUrl">
        <avatar-uploader @uploaded="imageUploaded" :avatar="dataForm.moduleImageUrl"/>
        <span class="el-upload__tip">小提示：建议上传大小为100*100(px)像素图</span>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch
          v-model="dataForm.isShow"
          :active-value="0"
          :inactive-value="1"
          active-color="#13ce66"
          inactive-color="#999"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" placeholder="排序"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import AvatarUploader from '@/components/uploader/avatar-uploader'
  import NewsCategorySelector from './modular-selector/news-category-selector'
  import NewsDetailSelector from './modular-selector/news-detail-selector'
  import CourseDetailSelector from './modular-selector/course-detail-selector'
  import ExamCateSelector from './modular-selector/exam-cate-selector'
  import DailyCateSelector from './modular-selector/daily-cate-selector'
  import ExamDetailSelector from './modular-selector/exam-detail-selector'
  import DeilyDetailSelector from './modular-selector/daily-detail-selector'

  export default {
    data() {
      return {
        loading: false,
        visible: false,
        moduleList: [],
        dataForm: {
          id: 0,
          moduleName: '',
          remark: '',
          isShow: 0,
          moduleId: '',
          params: '',
          paramsName: '',
          moduleImageUrl: '',
          sort: 0
        },
        selectedModule: null,
        dataRule: {
          moduleName: [
            {required: true, message: '首页分类不能为空', trigger: 'blur'}
          ],
          moduleId: [
            {required: true, message: '关联模块不能为空', trigger: 'blur'}
          ],
          sort: [{required: true, message: '排序不能为空', trigger: 'blur'}],
          moduleImageUrl: [
            {required: true, message: '图片不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
      AvatarUploader,
      NewsCategorySelector,
      NewsDetailSelector,
      CourseDetailSelector,
      ExamCateSelector,
      DailyCateSelector,
      ExamDetailSelector,
      DeilyDetailSelector
    },
    methods: {
      async init(id) {
        this.dataForm.id = id
        this.visible = true
        let isEdit = !!id
        if (isEdit) {
          await this.loadModuleList()
          await this.initCurrentModule(id)
        } else {
          this.selectedModule = null
          await this.loadModuleList()
          this.$nextTick(() => {
            this.$refs.dataForm.resetFields()
          })
        }
      },
      async loadModuleList() {
        let res = await this.$http({
          url: this.$http.adornUrl(`/sys/module/select`),
          method: 'post'
        })
        this.moduleList = res.data
      },
      async initCurrentModule(id) {
        let res = await this.$http({
          url: '/sys/indexModule/info/' + id,
          method: 'post'
        })

        this.$nextTick(() => {
          this.dataForm.id = id
          this.dataForm.moduleName = res.data.moduleName
          this.dataForm.params = res.data.params
          this.dataForm.isShow = res.data.isShow
          this.dataForm.sort = res.data.sort
          this.dataForm.moduleId = res.data.moduleId
          this.dataForm.moduleImageUrl = res.data.moduleImageUrl

          let mid = this.dataForm.moduleId
          this.selectedModule = this.moduleList.find(x => x.id === mid)
        })
      },
      selectParams(val) {
        this.$nextTick(() => {
          this.dataForm.params = val.id
          this.dataForm.paramsName = val.title
        })
      },
      moduleChange(val) {
        this.selectedModule = this.moduleList.find(x => x.id === val)
      },
      imageUploaded(file) {
        this.dataForm.moduleImageUrl = file.url
      },
      // 表单提交
      async dataFormSubmit() {
        if (this.selectedModule.isParams === 1 && this.dataForm.paramsName === '') {
          this.$notify.error({title: '失败', message: '详情不能为空'})
          return false
        }
        let data = this.dataForm
        let valid = await this.$refs['dataForm'].validate()
        if (!valid) return
        this.loading = true
        let res = await this.$http({
          url: `/sys/indexModule/${!this.dataForm.id ? 'save' : 'update'}`,
          method: 'post',
          data: data
        })
        if (res.code === 200) {
          this.visible = false
          this.$emit('refreshDataList')
          this.$notify.success({title: '成功', message: '保存成功'})
        } else {
          this.$notify.error({title: '失败', message: res.msg})
        }
        this.loading = false
      }
    }
  }
</script>
<style scoped>
  .avatar-uploader .el-upload {
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 6px;
    border: 1px dashed #d9d9d9;
  }

  .avatar {
    width: 176px;
    height: 176px;
    display: block;
    border-radius: 6px;
    border: 1px dashed #d9d9d9;
  }

  .avatar:hover {
    border-color: #409eff;
  }
</style>
